#!/bin/bash
# This script is released under the GNU General Public License 3.0
# Check the COPYING file included with this distribution

# bootloader_uefi
# writes kernel to UEFI as new boot option
#
# parameters (required)
#  CONFIG_LIST: One string items of defined FSspec
#
# returns 0 on success
# returns: 1 on failure
#
# writes menus and noise to STDERR

# location of other scripts to source
readonly SHAREDIR="$(dirname ${0})" || exit $?

# source bootloader commons
source "${SHAREDIR}"/bootloader_common.sh || exit $?

#########################################
## START: dialog functions/definitions ##

## END: dialog functions/definitions ##
#######################################

#####################
## begin execution ##

# check input
check_num_args "$(basename $0)" 1 $# || exit $?
CONFIG_LIST="${1}"

echo dump CONFIG_LIST="'${CONFIG_LIST}'" 1>&2

modprobe efivars
# check if booted through UEFI
efibootmgr -v 2>/dev/null 1>&2
if [ $? -ne 0 ]; then
	show_dialog --msgbox "Error: Couldn't read from UEFI. Did you boot through UEFI?" 0 0
	exit 1
fi

# mount everything, including cryptsetup
"${SHAREDIR}"/FSspec mountall "${CONFIG_LIST}" || exit $?

# get kernel version
KERNVER=$(getkernelversion) || exit $?

# kernelpath: path to kernel, relative to partition root
KERNELPATH="/boot/kernel-genkernel${KERNVER}"
# initrdpath: path to initrd
INITRDPATH="/boot/initramfs-genkernel${KERNVER}"

# get root partition
CONFIG_ITEM="$("${SHAREDIR}"/FSspec listfind "${CONFIG_LIST}" 'mountpoint' '/')" || exit $?
ROOT_PART="$("${SHAREDIR}"/FSspec parse "${CONFIG_ITEM}" 'partition')" || exit $?
# root partition encrypted?
ROOT_CRYPTTYPE="$("${SHAREDIR}"/FSspec parse "${CONFIG_ITEM}" 'crypttype')" || exit $?

# check for separate /boot partition
BOOT_PART=
if "${SHAREDIR}"/FSspec list_haskeyvalue "${CONFIG_LIST}" 'mountpoint' '/boot'; then
	# get boot partition
	CONFIG_ITEM="$("${SHAREDIR}"/FSspec listfind "${CONFIG_LIST}" 'mountpoint' '/boot')" || exit $?
	BOOT_PART="$("${SHAREDIR}"/FSspec parse "${CONFIG_ITEM}" 'partition')" || exit $?
	# fix paths
	KERNELPATH="${KERNELPATH#/boot}"
	INITRDPATH="${INITRDPATH#/boot}"
fi

# kernel path with \\ instead of /
KERNELPATH=${KERNELPATH//\//\\\\}
# initrd path with \ instead of /
INITRDPATH=${INITRDPATH//\//\\}

# get kernel params
KERNEL_PARAMS="$(getkernelparams "${ROOTPART}" "${BOOTDEV}" "${CRYPTTYPE}")" || exit $?

# kernelpart=
# # kernelpart: kernel partition, ex. /dev/sda2
# kernelpart="$(show_dialog --menu "Select the partition with the kernel (/boot)" 21 50 13 NONE - ${PARTS})" || exit $?
# PARTS="$(echo ${PARTS} | sed -e "s#${kernelpart}\ _##g")"
# [ "${kernelpart}" = "NONE" ] && return 1

# kernelpart as disk and trailing part-number
local kernelpartnu=$(expr match "${kernelpart}" '.*\([1-9][0-9]*\)')
local kernelpartdisk=${kernelpart:0: -${#kernelpartnu}}
# write to UEFI
echo "${KERNEL_PARAMS} initrd=${initrdpath}" | \
	iconv -f ascii -t ucs2 | \
	efibootmgr --create --gpt \
		--disk "${kernelpartdisk}" --part "${kernelpartnu}" \
		--label "Pentoo" \
		--loader "${kernelpath}" \
		--append-binary-args -
if [ $? -ne 0 ]; then
	show_dialog --msgbox "Error: Couldn't write to UEFI!" 0 0
	return 1
fi
show_dialog --msgbox "Success: Direct UEFI booting installed!" 0 0

exit 0
